// serviceRepair.js
const app = getApp()
const util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datetime: {
      startTime: util.formatdate(new Date((new Date().getFullYear()), (new Date().getMonth() - 6), (new Date().getDate()))),
      endTime: util.formatdate(new Date()),
      startDate: new Date().getTime(),
      endDate: new Date().getTime(),
      timeshow: false,
      currentDate: new Date().getTime(),//1574646440123
      minDate: new Date((new Date().getFullYear() - 1), 0, 1).getTime(),
      maxDate: new Date((new Date().getFullYear() + 2), 11, 31).getTime(),
      timeStatus: 0
    },
    pagenum: 1,
    status: 1, //1:待处理，2：已完成，0：已取消
    list: [],
    detail:[],
    detailShow:false,
  },
  // 确认时间
  timeConfirm(event) {
    var that = this;
    var date = new Date(event.detail);
    var time = util.formatdate(date);
    var datetime = that.data.datetime;
    var status = datetime.timeStatus;
    if (status == 0) {
      that.setData({
        ['datetime.startTime']: time,
        ['datetime.startDate']: event.detail,
        ['datetime.timeshow']: false,
        ['datetime.minDate']: event.detail,
      });
      if (datetime.startDate > datetime.endDate) {
        that.setData({
          ['datetime.endTime']: time,
          ['datetime.endDate']: event.detail,
        });
      }
    } else if (status == 1) {
      that.setData({
        ['datetime.endTime']: time,
        ['datetime.endDate']: event.detail,
        ['datetime.timeshow']: false
      });
    }
  },
  // 显示时间选择
  timeShow(e) {
    var status = e.currentTarget.dataset.status;
    this.setData({
      ['datetime.timeshow']: true,
      ['datetime.timeStatus']: status,
    });
    if (status == 0) {
      this.setData({
        ['datetime.minDate']: new Date((new Date().getFullYear() - 1), 0, 1).getTime(),
      })
    }
  },
  // 关闭时间选择
  timeClose() {
    this.setData({ ['datetime.timeshow']: false });
  },
  // 触发详情弹窗
  orderDetail: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var detail = that.data.list[index];
    that.setData({
      detailShow: true,
      detail: detail
    })
  },
  onClose() {
    this.setData({ detailShow: false });
  },
  orderlist: function () {
    var that = this;
    var data = {
      beginDate: that.data.datetime.startTime,
      endDate: that.data.datetime.endTime,
      status: that.data.status,
      page: that.data.pagenum,
      type:that.data.type
    };
    app.post('manage/manageRecord', data).then((res) => {
      if (res.code == 200) {
        var list = that.data.list;
        var dataArr = res.list;
        for(var i = 0; i<dataArr.length; i++){
          var file = {};
          var fileList = [];
          var imgList = dataArr[i].images.split(",");
          for (var item in imgList) {
            file = {
              'url': imgList[item],
              'isImage':true
            }
            fileList.push(file)
          };
          dataArr[i].images = fileList;
        }
        
        list = list.concat(dataArr)
        that.setData({
          list: list
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
    wx.stopPullDownRefresh()
  },
  // 修改报修状态
  updateStatus:function(id,status){
    var that = this;
    var data = {
      id: id, status: status
    };
    wx.showLoading({
      title: '加载中...',
    })
    app.post('manage/repairDevice', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "修改成功",
          icon: 'success',
          duration: 2000
        })
        this.setData({
          pagenum: 1,
          list:[]
        })
        this.orderlist();
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  // 完成保修
  finishTap: function (e) {
    var id = e.currentTarget.dataset.id;
    this.updateStatus(id, 2)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type ? options.type : app.globalData.userInfo.type
    this.setData({
      status: options.status,
      pagenum: 1,
      type:type
    })
    this.orderlist();
  },
  timeSearch: function () {
    this.setData({
      list: [],
      pagenum: 1
    })
    this.orderlist();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 请求最新数据
    this.setData({
      list: [],
      pagenum: 1
    })
    this.orderlist();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var pagenum = that.data.pagenum + 1; //获取当前页数并+1
    that.setData({
      pagenum: pagenum, //更新当前页数
    })
    that.orderlist();//重新调用请求获取下一页数据
  }
})